/**
 * @title VersionUtils.java
 * @package com.esun.util
 * @description 提供版本、渠道等信息
 * @author liugan
 * @date 2012-5-21
 * @copyright 深圳市易讯天空
 */
package com.esun.util;

import com.esun.cheetah.biz.setting.CheckUpgradeCommunicater.UpgradeInfo;
import com.esun.cheetah.config.Config;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;

/**
 * @title VersionUtils
 * @description 提供版本、渠道等信息
 * @author liugan
 */
public class VersionUtils {

	public static final String CHANNEL = "CHANNEL";

	private static final VersionUtils instance = new VersionUtils();

	private boolean mNeedUpgrade;
	
	// 是否强制更新
	private boolean mMustUpgrade;

	private UpgradeInfo mUpgradeInfo;

	private int isJiHuo = -1;
	
	private VersionUtils() {

	}

	public static VersionUtils getInstance() {
		return instance;
	}

	public String getChannel(Activity activity) {
		String channel = null;
		try {
			ApplicationInfo ai = activity.getPackageManager()
					.getApplicationInfo(activity.getPackageName(),
							PackageManager.GET_META_DATA);
			channel = String.valueOf(ai.metaData.getInt(CHANNEL));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return channel;
	}

	/**
	 * @title getVersion
	 * @description 获得版本号
	 * @return
	 */
	public int getVersion(Activity activity) {
		int versionCode = -1;
		try {
			// ---get the package info---
			PackageManager pm = activity.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(activity.getPackageName(), -1);
			versionCode = pi.versionCode;
		} catch (Exception e) {
			return -1;
		}
		
		return versionCode;
	}

	//获取是否已激活信息
	public int getIsActive(Context context){
		if(isJiHuo == -1){
			SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
			isJiHuo = preferences.getInt("isJiHuo", 1);
			if(isJiHuo == 0){
				final int latestSavedVersionCode = preferences.getInt("version_code_for_jihuo", -1);
				int currentVersionCode = 0;
				try {
					currentVersionCode = context.getPackageManager().getPackageInfo(Config.APP_PACKAGE_INFO, 0).versionCode;
				} catch (NameNotFoundException e) {
					e.printStackTrace();
				}
				if(latestSavedVersionCode != currentVersionCode && currentVersionCode > 0){
					isJiHuo = 1;
					preferences.edit().putInt("version_code_for_jihuo", currentVersionCode).commit();
				}
			}
		}
		return isJiHuo;
	}
	//设置该软件已激活
	public void setActived(Activity activity){
		if(isJiHuo != 0){
			isJiHuo = 0;
			SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
			preferences.edit().putInt("isJiHuo", isJiHuo).commit();
		}
		
	}
	
	//获取IMEI号码
	public String getSerial(Context context){
		String serial = null;
		TelephonyManager telephonyManager=(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		serial = telephonyManager.getDeviceId();
		return serial;
	}
	
	public String getScreenreso(Activity activity){
		DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.widthPixels+"X"+dm.heightPixels;
	}
	/**
	 * @title needUpgrade
	 * @description
	 * @return
	 */
	public boolean needUpgrade() {
		return mNeedUpgrade;
	}

	public void setNeedUpgrade(boolean needUpgrade) {
		mNeedUpgrade = needUpgrade;
	}

	public boolean ismMustUpgrade() {
		return mMustUpgrade;
	}

	public void setmMustUpgrade(boolean mMustUpgrade) {
		this.mMustUpgrade = mMustUpgrade;
	}

	/**
	 * @title getUpgradeInfo
	 * @description 获取更新信息
	 * @return
	 */
	public UpgradeInfo getUpgradeInfo() {
		return mUpgradeInfo;
	}

	public void setUpgradeInfo(UpgradeInfo info) {
		this.mUpgradeInfo = info;
		
	}

}
